package com.sjy.project.fym.apply.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.manager.util.SessionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sjy.framework.aspectj.lang.annotation.Log;
import com.sjy.framework.aspectj.lang.enums.BusinessType;
import com.sjy.project.fym.apply.domain.FyGoodsApply;
import com.sjy.project.fym.apply.domain.FyGoodsApplyHis;
import com.sjy.project.fym.apply.service.IFyGoodsApplyHisService;
import com.sjy.project.fym.apply.service.IFyGoodsApplyService;
import com.sjy.project.fym.place.domain.FyPlace;

import cn.hutool.core.date.BetweenFormater.Level;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import com.sjy.framework.web.controller.BaseController;
import com.sjy.framework.web.domain.AjaxResult;
import com.sjy.common.utils.DateUtils;
import com.sjy.common.utils.poi.ExcelUtil;
import com.sjy.common.utils.security.ShiroUtils;
import com.sjy.framework.web.page.TableDataInfo;

/**
 * 防疫物资申请Controller
 * 
 * @author ljb
 * @date 2022-04-21
 */
@Controller
@RequestMapping("/fym/apply")
public class FyGoodsApplyController extends BaseController
{
    private String prefix = "fym/apply";

    @Autowired
    private IFyGoodsApplyService fyGoodsApplyService;
    @Autowired
    private IFyGoodsApplyHisService hisService;

    @RequiresPermissions("fym:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询防疫物资申请列表
     */
    @RequiresPermissions("fym:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FyGoodsApply fyGoodsApply)
    {
        startPage();
        List<FyGoodsApply> list = fyGoodsApplyService.selectFyGoodsApplyList(fyGoodsApply);
        return getDataTable(list);
    }

    /**
     * 导出防疫物资申请列表
     */
    @RequiresPermissions("fym:apply:export")
    @Log(title = "防疫物资申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FyGoodsApply fyGoodsApply)
    {
        List<FyGoodsApply> list = fyGoodsApplyService.selectFyGoodsApplyList(fyGoodsApply);
        ExcelUtil<FyGoodsApply> util = new ExcelUtil<FyGoodsApply>(FyGoodsApply.class);
        return util.exportExcel(list, "防疫物资申请数据");
    }

    /**
     * 新增防疫物资申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存防疫物资申请
     */
    @RequiresPermissions("fym:apply:add")
    @Log(title = "防疫物资申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FyGoodsApply fyGoodsApply)
    {
        return toAjax(fyGoodsApplyService.insertFyGoodsApply(fyGoodsApply));
    }
    
//    @GetMapping("/detail/{applyId}")
//    public String detail(@PathVariable("applyId") Long applyId, ModelMap mmap)
//    {
//        mmap.put("fyGoodsApply", fyGoodsApplyService.selectFyGoodsApplyByApplyId(applyId));
//        return prefix + "/detail";
//    }
    @Log(title = "防疫物资申请", businessType = BusinessType.UPDATE)
    @PostMapping("/approval")
    @ResponseBody
    public AjaxResult approval(Long applyId)
    {
    	System.out.println(applyId);
    	FyGoodsApply fyGoodsApply = fyGoodsApplyService.selectFyGoodsApplyByApplyId(applyId);
		fyGoodsApply.setUpdateBy(ShiroUtils.getLoginName());
		fyGoodsApply.setUpdateTime(DateUtils.getNowDate());
		

		//插入审批记录
		FyGoodsApplyHis fyGoodsApplyHis = new FyGoodsApplyHis();
		fyGoodsApplyHis.setApplyId(fyGoodsApply.getApplyId());
		fyGoodsApplyHis.setLiushuiNumber(fyGoodsApply.getLiushuiNumber());
		fyGoodsApplyHis.setRemark(fyGoodsApply.getRemark());
		fyGoodsApplyHis.setCreateByName(ShiroUtils.getSysUser().getUserName());
		fyGoodsApplyHis.setCreateBy(ShiroUtils.getLoginName());
		fyGoodsApplyHis.setCreateTime(new Date());
		fyGoodsApplyHis.setUseTime(DateUtil.formatBetween(fyGoodsApply.getCreateTime(), fyGoodsApply.getUpdateTime(), Level.MINUTE));
    	if("1".equals(fyGoodsApply.getApplyStatus())) {
    		fyGoodsApply.setApplyStatus("2");
    		fyGoodsApply.setApplyNode("在办中");
    		fyGoodsApply.setNextPerson("待物资配送");
    		//插入审批记录
    		fyGoodsApplyHis.setApplyNode(fyGoodsApply.getApplyNode());
    		fyGoodsApplyHis.setApplyStatus(fyGoodsApply.getApplyStatus());
    		
    		hisService.insertFyGoodsApplyHis(fyGoodsApplyHis);
    		return toAjax(fyGoodsApplyService.updateFyGoodsApply(fyGoodsApply));
    	}
    	if("2".equals(fyGoodsApply.getApplyStatus())) {
    		fyGoodsApply.setApplyStatus("3");
    		fyGoodsApply.setApplyNode("已受理");
    		fyGoodsApply.setNextPerson("待物资接收");
    		//插入审批记录
    		fyGoodsApplyHis.setApplyNode(fyGoodsApply.getApplyNode());
    		fyGoodsApplyHis.setApplyStatus(fyGoodsApply.getApplyStatus());
    		fyGoodsApplyHis.setCarNumber(fyGoodsApply.getCarNumber());
    		fyGoodsApplyHis.setCarPersonName(fyGoodsApply.getCarPersonName());
    		
    		hisService.insertFyGoodsApplyHis(fyGoodsApplyHis);
    		return toAjax(fyGoodsApplyService.updateFyGoodsApply(fyGoodsApply));
    	}
    	return toAjax(0);
    	
    }

    
    /**
     * 调度  增加车辆，人员，备注信息
     */
    @GetMapping("/detail/{applyId}")
    public String detail(@PathVariable("applyId") Long applyId, ModelMap mmap,HttpServletRequest request)
    {
    	FyGoodsApply fyGoodsApply = fyGoodsApplyService.selectFyGoodsApplyByApplyId(applyId);
        mmap.put("fyGoodsApply", fyGoodsApply);
        return prefix + "/approval";
    }
    /**
     * 修改防疫物资申请
     */
    @RequiresPermissions("fym:apply:edit")
    @GetMapping("/edit/{applyId}")
    public String edit(@PathVariable("applyId") String applyId, ModelMap mmap)
    {
    	if(applyId.startsWith("--")) {
    		applyId = applyId.replace("--", "");
    		FyGoodsApply fyGoodsApply = fyGoodsApplyService.selectFyGoodsApplyByApplyId(Long.parseLong(applyId));
	        mmap.put("fyGoodsApply", fyGoodsApply);
	        return prefix + "/approval";
    	}
        FyGoodsApply fyGoodsApply = fyGoodsApplyService.selectFyGoodsApplyByApplyId(Long.parseLong(applyId));
        mmap.put("fyGoodsApply", fyGoodsApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存防疫物资申请
     */
    @RequiresPermissions("fym:apply:edit")
    @Log(title = "防疫物资申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FyGoodsApply fyGoodsApply,String approvalFalg)
    {
    	int flag = fyGoodsApplyService.updateFyGoodsApply(fyGoodsApply);
    			
    	if("1".equals(approvalFalg)) {
    		//如果是审批页面的修改，录入信息的同时 调用approval 接口
            approval(fyGoodsApply.getApplyId());
    	}
        
        
        return toAjax(flag);
    }

    /**
     * 删除防疫物资申请
     */
    @RequiresPermissions("fym:apply:remove")
    @Log(title = "防疫物资申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fyGoodsApplyService.deleteFyGoodsApplyByApplyIds(ids));
    }
}
